/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
 * main.c
 * Copyright (C) zhoubo 2009 <zhoubo.mail@gmail.com>
 * 
 * main.c is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * main.c is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>

#include <config.h>

#include <gtk/gtk.h>

#include <glade/glade.h>

#define LENSTR 20



/*
 * Standard gettext macros.
 */
#ifdef ENABLE_NLS
#  include <libintl.h>
#  undef _
#  define _(String) dgettext (PACKAGE, String)
#  ifdef gettext_noop
#    define N_(String) gettext_noop (String)
#  else
#    define N_(String) (String)
#  endif
#else
#  define textdomain(String) (String)
#  define gettext(String) (String)
#  define dgettext(Domain,Message) (Message)
#  define dcgettext(Domain,Message,Type) (Message)
#  define bindtextdomain(Domain,Directory) (Domain)
#  define _(String) (String)
#  define N_(String) (String)
#endif



#include "callbacks.h"

/* For testing propose use the local (not installed) glade file */
/* #define GLADE_FILE PACKAGE_DATA_DIR"/login/glade/login.glade" */
#define GLADE_FILE "src/login.glade"
	
#include "inc.h"

typedef struct _infolist
{
    char ip[25];
    long time;
    long freein,freeout;
    long in,out,tot;
    char mode;
    double money;
    }Info;
typedef struct _userinfo
{
    char is;
    char name[20];
    char pwd[20];

    }Userinfo;
Userinfo userinfo;
static GtkWidget *window;
static GtkWidget *userText;
static GtkWidget *pwdText;
static GtkWidget *ipText;
static GtkWidget *ethCombo;
static GtkWidget *checkPwd;
static GtkWidget *checkLog;
static GtkWidget *aboutdialog;

gboolean ispwd;
gboolean islog;
	const gchar* usename;
	const gchar* usepwd;
	const gchar* eth;

const gchar *ip="10.0.0.55";
char strlist[10][7];
int listnum=0;
//-----------------------------
int event_logout();
int event_login();
void create_popup_menu(GtkWidget *menu);
int file_write_userinfo(Userinfo user);
int file_read_userinfo(Userinfo *user);
void event_menu_popup_show_srun(GtkStatusIcon *status_icon,GdkEvent *event,GtkWidget *data);
void check_callback(GtkWidget *checkbutton,gpointer data);
void show_about_dialog();
void call_quit_choice();
//-----------------------------
void main_window_hide()
{
gtk_widget_hide_all (window);
}
gboolean main_window_show()
{
gtk_widget_show_all(window); 
return TRUE;
}
int event_logout()
{
char *buffer="./auth logout\n";
system(buffer);
gtk_widget_hide_all (window);
return 0;
}
int event_login()
{
	gchar buffer[100];



	usename=gtk_entry_get_text(GTK_ENTRY(userText));
	usepwd=gtk_entry_get_text(GTK_ENTRY(pwdText));
	ip=gtk_entry_get_text(GTK_ENTRY(ipText));
	eth=gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(ethCombo)->entry));
	bzero(buffer,sizeof(buffer));
	strcat(buffer,eth);
	strcat(buffer,"\n");
	strcat(buffer,ip);
	strcat(buffer,"\n");

	strcat(buffer,usename);
	strcat(buffer,"\n");
	strcat(buffer,usepwd);
	strcat(buffer,"\n");
	if(ispwd==FALSE)
	{
		strcpy(userinfo.pwd,"");
	}
	else
			strcpy(userinfo.pwd,usepwd);
	strcpy(userinfo.name,usename);
	file_write_userinfo (userinfo);

	
	//IniFile *ini;
//	ini=ini_file_new();
//	ini_file_write_string(ini,"SYSTEM","UserName",usename);
//	ini_file_write_string(ini,"SYSTEM","Pwd",);
//	ini_file_write_file(ini,"info.ini");
//	ini_file_free(ini);

FILE *fpout;
if((fpout=popen("~/auth","w"))==NULL)
{
return 0;
}

fputs(buffer,fpout);
if(pclose(fpout)==-1)
{
return 0;


}
gtk_widget_hide_all (window);
return 0;

}
void event_exit()
{
//event_logout ();
gtk_main_quit();
}
void getlistofeth()
{
char *delim=":";
char buffer[2000];

char *p;
char *k;
listnum=0;
int fd,size;
fd=open(ETHPATH,O_RDONLY);
size=read(fd,buffer,sizeof(buffer));
close(fd);
bzero(strlist,sizeof(strlist));
for(p=strtok(buffer,delim);p!=NULL;p=strtok(NULL,delim))
{
    if((k=strstr(p,"eth"))==NULL)
    k=strstr(p,"wlan");
    if(k!=NULL)
    {
    strcpy(strlist[listnum],k);
    listnum++;
    }
}
}
GtkWidget *creat_list_eth(gchar *ethname,gchar *flag)
{
GtkWidget *item;
GtkWidget *hbox;
GtkWidget *image;
GtkWidget *label;

hbox=gtk_hbox_new(FALSE,0);
if(strcmp(flag,"eth")==0)
	image=gtk_image_new_from_file(IMG_PATH_ETH);
else if(strcmp(flag,"wlan")==0)
	image=gtk_image_new_from_file(IMG_PATH_WLAN);
label=gtk_label_new(ethname);

gtk_box_pack_start(GTK_BOX(hbox),image,FALSE,FALSE,2);
gtk_box_pack_start(GTK_BOX(hbox),label,FALSE,FALSE,2);
item=gtk_list_item_new();
gtk_container_add(GTK_CONTAINER(item),hbox);
gtk_widget_show_all(item);
return item;
}

int main( int argc, char *argv[] )
{
int i;
	GtkStatusIcon* icon;
        GtkWidget *menu;
	

GtkWidget *userLabel;

GtkWidget *pwdLabel;
GtkWidget *ipLabel;


GtkWidget *ethLabel;

GtkWidget *vbox1;
GtkWidget *vbox2;

GtkWidget *vboxmain;

GtkWidget *hboxuser;
GtkWidget *hboxpwd;
GtkWidget *hboxip;
GtkWidget *hboxeth;
GtkWidget *hboxcheck;
GtkWidget *hboxbutton;

GtkWidget *item;

GtkWidget *frameuser;
GtkWidget *frameip;



GtkWidget *buttonLogin;
GtkWidget *buttonLogout;
GtkWidget *buttonQuit;
ispwd=FALSE;
islog=FALSE;
	//---
	file_read_userinfo (&userinfo);
	//---
gtk_init (&argc, &argv);
window=gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_signal_connect (GTK_OBJECT (window), "delete_event",GTK_SIGNAL_FUNC ( main_window_hide), NULL);
	
gtk_container_set_border_width (GTK_CONTAINER (window), 10);
gtk_window_set_title(GTK_WINDOW(window),"Colanet登录器");//设置窗体的标题
gtk_window_set_position(GTK_WINDOW(window),GTK_WIN_POS_CENTER);
menu=gtk_menu_new();
create_popup_menu(menu);
icon = gtk_status_icon_new_from_file (IMG_TICON);
        gtk_status_icon_set_tooltip(icon,"Colanet登录器");
        g_signal_connect((gpointer)icon, "button-press-event",GTK_SIGNAL_FUNC(event_menu_popup_show_srun),menu);
//        g_signal_connect((gpointer)icon, "activate", G_CALLBACK(callback_icon), NULL);
	
	
	userLabel=gtk_label_new("用户名");
	userText=gtk_entry_new();

	gtk_entry_set_text(GTK_ENTRY(userText),userinfo.name);
	
	pwdLabel=gtk_label_new("密码");
	pwdText=gtk_entry_new();
	gtk_entry_set_visibility(GTK_ENTRY(pwdText),FALSE);//密码显示
	gtk_entry_set_text(GTK_ENTRY(pwdText),userinfo.pwd);
	
	hboxuser=gtk_hbox_new(FALSE,0);
	hboxpwd=gtk_hbox_new(FALSE,0);
	vbox1=gtk_vbox_new(FALSE,0);

	gtk_box_pack_start(GTK_BOX(hboxuser),userLabel,FALSE,FALSE,5);
	gtk_box_pack_end(GTK_BOX(hboxuser),userText,FALSE,FALSE,5);

	gtk_box_pack_start(GTK_BOX(hboxpwd),pwdLabel,FALSE,FALSE,5);
	gtk_box_pack_end(GTK_BOX(hboxpwd),pwdText,FALSE,FALSE,5);
	gtk_box_pack_start(GTK_BOX(vbox1),hboxuser,FALSE,FALSE,5);
	gtk_box_pack_end(GTK_BOX(vbox1),hboxpwd,FALSE,FALSE,5);

frameuser=gtk_frame_new("用户信息");
	gtk_container_add(GTK_CONTAINER(frameuser),vbox1);
//网络信息

	ipLabel=gtk_label_new("域地址");
	ipText=gtk_entry_new();
	gtk_entry_set_text(GTK_ENTRY(ipText),ip);
	//网卡
	ethLabel=gtk_label_new("网卡");
	ethCombo=gtk_combo_new();
	gtk_widget_set_size_request(GTK_WIDGET(ethCombo),160,28);

 getlistofeth();
	for(i=0;i<listnum;i++)
	{
		if(strlist[i][0]=='e')
		{
	item=creat_list_eth(strlist[i],"eth");
	gtk_combo_set_item_string(GTK_COMBO(ethCombo),GTK_ITEM(item),strlist[i]);
	gtk_container_add(GTK_CONTAINER(GTK_COMBO(ethCombo)->list),item);

		}
		else
		{
	item=creat_list_eth(strlist[i],"wlan");
	gtk_combo_set_item_string(GTK_COMBO(ethCombo),GTK_ITEM(item),strlist[i]);
	gtk_container_add(GTK_CONTAINER(GTK_COMBO(ethCombo)->list),item);

		}

	}

	hboxip=gtk_hbox_new(FALSE,0);
	hboxeth=gtk_hbox_new(FALSE,0);
	vbox2=gtk_vbox_new(FALSE,0);

	gtk_box_pack_start(GTK_BOX(hboxip),ipLabel,FALSE,FALSE,5);
	gtk_box_pack_end(GTK_BOX(hboxip),ipText,FALSE,FALSE,5);

	gtk_box_pack_start(GTK_BOX(hboxeth),ethLabel,FALSE,FALSE,5);
	gtk_box_pack_end(GTK_BOX(hboxeth),ethCombo,FALSE,FALSE,5);
	gtk_box_pack_start(GTK_BOX(vbox2),hboxip,FALSE,FALSE,5);
	gtk_box_pack_end(GTK_BOX(vbox2),hboxeth,FALSE,FALSE,5);

frameip=gtk_frame_new("网络信息");
	gtk_container_add(GTK_CONTAINER(frameip),vbox2);
	        //
//button = gtk_button_new_with_label ("Hello Ubuntu!");
//gtk_signal_connect (GTK_OBJECT (button), "clicked",GTK_SIGNAL_FUNC (hello), NULL);
//gtk_signal_connect_object (GTK_OBJECT (button), "clicked",GTK_SIGNAL_FUNC (gtk_widget_destroy),GTK_OBJECT (window));
vboxmain=gtk_vbox_new(FALSE,0);
gtk_box_pack_start(GTK_BOX(vboxmain),frameuser,FALSE,FALSE,5);
gtk_box_pack_start(GTK_BOX(vboxmain),frameip,FALSE,FALSE,5);
//---check
checkPwd=gtk_check_button_new_with_label("保存密码");
checkLog=gtk_check_button_new_with_label("自动登录");

hboxcheck=gtk_hbox_new(FALSE,0);
gtk_box_pack_start(GTK_BOX(hboxcheck),checkPwd,FALSE,FALSE,5);
gtk_box_pack_end(GTK_BOX(hboxcheck),checkLog,FALSE,FALSE,5);
gtk_box_pack_start(GTK_BOX(vboxmain),hboxcheck,FALSE,FALSE,5);
g_signal_connect(G_OBJECT(checkPwd),"released",G_CALLBACK(check_callback),(gpointer)1);
g_signal_connect(G_OBJECT(checkLog),"released",G_CALLBACK(check_callback),(gpointer)2);

//--button

buttonLogin=gtk_button_new_with_label("连线");
	gtk_signal_connect (GTK_OBJECT (buttonLogin), "clicked",GTK_SIGNAL_FUNC (event_login), NULL);
buttonLogout=gtk_button_new_with_label("离线");
	gtk_signal_connect (GTK_OBJECT (buttonLogout), "clicked",GTK_SIGNAL_FUNC (event_logout), NULL);
buttonQuit=gtk_button_new_with_label("退出");
	gtk_signal_connect (GTK_OBJECT (buttonQuit), "clicked",GTK_SIGNAL_FUNC (call_quit_choice), NULL);

hboxbutton=gtk_hbox_new(FALSE,0);

gtk_box_pack_start(GTK_BOX(hboxbutton),buttonLogin,TRUE,TRUE,10);
gtk_box_pack_start(GTK_BOX(hboxbutton),buttonLogout,TRUE,TRUE,10);
gtk_box_pack_end(GTK_BOX(hboxbutton),buttonQuit,TRUE,TRUE,10);
gtk_box_set_homogeneous(GTK_BOX(hboxbutton),TRUE);
gtk_box_pack_start(GTK_BOX(vboxmain),hboxbutton,FALSE,FALSE,5);



	gtk_container_add (GTK_CONTAINER (window), vboxmain);



//gtk_widget_show (frameuser);
gtk_window_set_icon_from_file(GTK_WINDOW(window),IMG_ICON,NULL);
	//

	//
gtk_widget_show_all(window);   /*显示一个窗口*/
if(userinfo.is==1)
	{
		ispwd=!ispwd;
		gtk_toggle_button_set_active (checkPwd,TRUE);
			
		//gtk_widget_set_style (checkPwd,"active");
		//select_row_callback
	}
gtk_main();   /*进入主循环*/
return(0);
}

void create_popup_menu(GtkWidget *menu)
{
    GtkWidget *showmain,*login,*logout,*about,*quit;
    showmain=gtk_menu_item_new_with_label("显示界面");
    login=gtk_menu_item_new_with_label("  连线");
    logout=gtk_menu_item_new_with_label("  离线");
    about=gtk_menu_item_new_with_label("  关于");
    quit=gtk_menu_item_new_with_label("  退出");
    //加入菜单
    gtk_menu_shell_append(GTK_MENU_SHELL(menu),showmain);
    gtk_menu_shell_append(GTK_MENU_SHELL(menu),login);
    gtk_menu_shell_append(GTK_MENU_SHELL(menu),logout);
    gtk_menu_shell_append(GTK_MENU_SHELL(menu),about);
    gtk_menu_shell_append(GTK_MENU_SHELL(menu),quit);
	g_signal_connect(G_OBJECT(showmain),"activate",G_CALLBACK(main_window_show),NULL);
	g_signal_connect(G_OBJECT(login),"activate",G_CALLBACK(event_login),NULL);
	g_signal_connect(G_OBJECT(logout),"activate",G_CALLBACK(event_logout),NULL);
	g_signal_connect(G_OBJECT(about),"activate",G_CALLBACK(show_about_dialog ),NULL);
	g_signal_connect(G_OBJECT(quit),"activate",G_CALLBACK(call_quit_choice),NULL);
	

    gtk_widget_show_all(menu);



    }
void event_menu_popup_show_srun(GtkStatusIcon *status_icon,GdkEvent *event,GtkWidget *data)
{
    GdkEventButton *eventButton;
    eventButton=(GdkEventButton*)event;
    if(eventButton->button==1)//单击左键
	{
		main_window_show();		
	}
else  if(eventButton->button==3)//单击右键
    {

    gtk_menu_popup(GTK_MENU(data),NULL,NULL,NULL,NULL,eventButton->button,eventButton->time);
    }
}

int file_read_userinfo(Userinfo *user)
{
    FILE *fp;
    if((fp=fopen("info.dat","r+"))!=NULL)
    {
    user->is=fgetc(fp);
    fgets(user->name,LENSTR,fp);
    fgets(user->pwd,LENSTR,fp);
    fclose(fp);
	user->name[strlen(user->name)-1]='\0';
	user->pwd[strlen(user->pwd)-1]='\0';

    if(user->is==0)
    {
        return 0;//不保存密码
    }
    else
    {
       return 1;
        }

    }
    return -1;
    }

int file_write_userinfo(Userinfo user)
{
    FILE *fp;
    if((fp=fopen("info.dat","w"))!=NULL)
    {fputc(user.is,fp);
    fputs(user.name,fp);
	fputc('\n',fp);
    fputs(user.pwd,fp);
		fputc('\n',fp);

		fclose(fp);
    return 0;
    }
    else
    return -1;
}
//--------callback--------
void check_callback(GtkWidget *checkbutton,gpointer data)
{
switch((int)data)
	{
		case 1:
			ispwd=!ispwd;
			userinfo.is=(char)ispwd;
		break;
		case 2:
			islog=!islog;
		break;
	}
}
//-------关于对话框--------
GtkWidget *creat_about_dialog()
{
GtkWidget *window;
	GtkWidget *label;
window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
	gtk_window_set_position(GTK_WINDOW(window),GTK_WIN_POS_CENTER);
	gtk_window_set_title(GTK_WINDOW(window),"关于Colanet");//设置窗体的标题
	gtk_window_set_icon_from_file(GTK_WINDOW(window),IMG_ICON,NULL);
return window;
}
void show_about_dialog()
{
	aboutdialog=creat_about_dialog ();
	gtk_widget_show(aboutdialog);
}
//--------------------------------
//--------退出确认----------------
void on_ok_clicked()
{
gtk_main_quit ();
}
void on_cancel_clicked()
{}
void call_quit_choice()
{GtkWidget * image;
GtkWidget * label;
GtkWidget * btnok;
GtkWidget * btncancel;
GtkWidget * window;
GtkWidget * vbox;
	
GtkWidget * hbox;
GtkWidget * bbox;

	GtkWidget * sep;
	window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
	//g_signal_connect(G_OBJECT(window),"destroy",G_CALLBACK(gtk_main_quit),NULL);
	
	gtk_window_set_position(GTK_WINDOW(window),GTK_WIN_POS_CENTER);
	gtk_window_set_title(GTK_WINDOW(window),"Colanet退出提示");//设置窗体的标题
	gtk_window_set_icon_from_file(GTK_WINDOW(window),IMG_ICON,NULL);
	
image=gtk_image_new_from_stock(GTK_STOCK_DIALOG_WARNING,GTK_ICON_SIZE_DIALOG);

vbox=gtk_vbox_new(FALSE,0);
hbox=gtk_hbox_new(FALSE,0);
bbox=gtk_hbox_new(FALSE,0);
	gtk_container_add(GTK_CONTAINER(window),vbox);
	
label=gtk_label_new("是否要真的退出Coloanet登录器\n");
btnok=gtk_button_new_with_label("是");
g_signal_connect(G_OBJECT(btnok),"clicked",G_CALLBACK(on_ok_clicked ),NULL);
//g_signal_connect_swapped(G_OBJECT(btnok),"clicked",G_CALLBACK(gtk_widget_destroy),window);
gtk_box_pack_start(GTK_BOX(bbox),btnok,TRUE,TRUE,10);


btncancel=gtk_button_new_with_label("否");
//g_signal_connect(G_OBJECT(btncancel),"clicked",G_CALLBACK(gtk_widget_destroy ),NULL);
g_signal_connect_swapped(G_OBJECT(btncancel),"clicked",G_CALLBACK(gtk_widget_destroy),window);
gtk_box_pack_start(GTK_BOX(bbox),btncancel,TRUE,TRUE,10);

gtk_box_set_homogeneous(GTK_BOX(bbox),TRUE);

gtk_box_pack_start(GTK_BOX(hbox),image,FALSE,FALSE,5);
gtk_box_pack_start(GTK_BOX(hbox),label,FALSE,FALSE,5);



gtk_box_pack_start(GTK_BOX(vbox),hbox,FALSE,FALSE,5);
sep=gtk_hseparator_new();
gtk_box_pack_start(GTK_BOX(vbox),sep,FALSE,FALSE,5);
gtk_box_pack_start(GTK_BOX(vbox),bbox,FALSE,FALSE,5);

	

gtk_widget_show_all(window);
}
